const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    //往日训练总数
    trainDayCount: [],
    trainPlan: [],
    currentDay: '',
    //当前显示的对象信息
    trainInfo: {
      trainNote: '',
      courseTrains: [],
      trainTypes: [],
      trainMovements: [],
      bodyMeasurements: [],
      pictures: []
    },

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: res => {
              app.globalData.userInfo = res.userInfo;
              that.setData({
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    });
    const openId = app.globalData.openId;
    const url = app.globalData.url;
    //提取今天的训练数据
    wx.request({
      url: url + '/train/trainDayList/' + openId,
      success: function(response) {
        if (response.data.success) {
          const trainDayRecords = response.data.content;
          const currentDay = trainDayRecords[trainDayRecords.length - 1];
          that.setData({
            currentDay: currentDay,
            trainDayCount: trainDayRecords
          });
          //提取最后一天的训练
          wx.request({
            url: url + '/train/dailyTrain',
            data: {
              openId: openId,
              trainDate: currentDay
            },
            method: 'POST',
            success: function(response) {
              if (response.data.success) {
                const dailyTrain = response.data.content;
                const bodyMeasurements = dailyTrain.bodyMeasurements;
                if (bodyMeasurements && bodyMeasurements.length > 0) {
                  bodyMeasurements.forEach(function(item) {
                    const dislayName = item.name + '(' + item.unit + ')';
                    const dislayName2 = dislayName.length > 6 ? (dislayName.substring(0, 3) + '...') : dislayName;
                    item.dislayName = dislayName2;
                  });
                }
                that.setData({
                  trainInfo: dailyTrain
                });
              }
            }
          });
        } else {
          wx.showToast({
            title: response.data.message,
            icon: 'none',
            duration:3000
          })
        }
      }
    })
  },
  changeDay: function(e) {
    const that = this;
    const currentIndex = e.detail.current;
    const trainDayCount = that.data.trainDayCount;
    const selectDay = trainDayCount[currentIndex];
    const url = app.globalData.url;
    const openId = app.globalData.openId;
    that.setData({
      currentDay: selectDay
    });
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function() {
      wx.hideLoading();
      wx.request({
        url: url + '/train/dailyTrain',
        data: {
          openId: openId,
          trainDate: selectDay
        },
        method: 'POST',
        success: function(response) {
          if (response.data.success) {
            const dailyTrain = response.data.content;
            const bodyMeasurements = dailyTrain.bodyMeasurements;
            if (bodyMeasurements && bodyMeasurements.length > 0) {
              bodyMeasurements.forEach(function(item) {
                const dislayName = item.name + '(' + item.unit + ')';
                const dislayName2 = dislayName.length > 6 ? (dislayName.substring(0, 3) + '...') : dislayName;
                item.dislayName = dislayName2;
              });
            }
            that.setData({
              trainInfo: dailyTrain
            });
          }
        }
      });
    }, 1000);

  },
  toPic: function() {
    wx.showToast({
      title: '功能正在开发',
      icon: 'none'
    })
  },
  toCourseTrain: function() {
    const courseTrains = this.data.trainInfo.courseTrains || [];
    const currentDay = this.data.currentDay;
    wx.setStorageSync("courseTrains", JSON.stringify(courseTrains));
    wx.setStorageSync("trainId", this.data.trainInfo.id);
    wx.navigateTo({
      url: '/pages/courseTrain/courseTrain',
    });
  },
  toPlan: function() {
    const trainPlan = this.data.trainPlan;
    wx.setStorageSync("trainPlan", JSON.stringify(trainPlan));
    wx.navigateTo({
      url: '/pages/trainPlan/trainPlan',
    })
  },
  toNote: function() {
    const trainNote = this.data.trainInfo.trainNote;
    const currentDay = this.data.currentDay;
    wx.setStorageSync("trainNote", trainNote);
    wx.setStorageSync("trainId", this.data.trainInfo.id);
    wx.navigateTo({
      url: '/pages/fitnessNote/fitnessNote',
    });
  },
  toTrainType: function() {
    const trainTypes = this.data.trainInfo.trainTypes;
    const currentDay = this.data.currentDay;
    wx.setStorageSync("trainTypes", JSON.stringify(trainTypes));
    wx.setStorageSync("trainId", this.data.trainInfo.id);
    wx.navigateTo({
      url: '/pages/trainType/trainType',
    });
  },
  toTrainMovement: function() {
    const trainMovements = this.data.trainInfo.trainMovements;
    const currentDay = this.data.currentDay;
    wx.setStorageSync("trainMovements", JSON.stringify(trainMovements));
    wx.setStorageSync("trainDate", currentDay);
    wx.navigateTo({
      url: '/pages/trainMovement/trainMovement',
    });
  },
  toMeasurement: function() {
    const bodyMeasurements = this.data.trainInfo.bodyMeasurements;
    const currentDay = this.data.currentDay;
    wx.setStorageSync("bodyMeasurements", JSON.stringify(bodyMeasurements));
    wx.setStorageSync("trainDate", currentDay);
    wx.navigateTo({
      url: '/pages/bodyMeasurement/bodyMeasurement',
    });
  }
})